//
//  LayoutCellBase.m
//  ebook
//
//  Created by 吴超飞 on 2017/3/14.
//  Copyright © 2017年 Moton. All rights reserved.
//

#import "LayoutCellBase.h"
#import "LayoutCellTable.h"
#import "EbookNetRequest.h"
#import "StoreBookModel.h"
#import "NativeSortNameManage.h"

@implementation LayoutCellBase

- (void)drawRect:(CGRect)rect {
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.moreBtn];
}

-(UILabel *)titleLabel{
    
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(LayoutSpaceOut, 0, MTScreenW-2*LayoutSpaceOut, TitleHeight)];
    }
    return _titleLabel;
}

-(UIButton *)moreBtn{
    
    if (!_moreBtn) {
        _moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(LayoutSpaceOut, 0, MTScreenW-2*LayoutSpaceOut, TitleHeight)];
        [_moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
       
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(_moreBtn.frame)-50, 0, 50, TitleHeight)];
        titleLabel.text = @"更多";
        [_moreBtn addSubview:titleLabel];
        
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(_moreBtn.frame)-15, (TitleHeight-15)/2, 15, 15)];
        iv.image = [UIImage imageNamed:@"book_arrow"];;
        [_moreBtn addSubview:iv];
        
    }
    return _moreBtn;
}

-(void)setLayOutType:(LayoutType *)layOutType{
    
    _layOutType = layOutType;
    self.titleLabel.text = layOutType.title;
    self.moreBtn.hidden = !layOutType.more;
    
    if(_dataArray.count>0)return;
    
    NSString *url = [NSString stringWithFormat:self.layOutType.api,self.bookCount];
    url = [url stringByAppendingString:@"&unencry=1"];
    //TODO 需加密
    
    [EbookNetRequest getUrl:url success:^(int stateCode, NSString *successMsg, NSDictionary *dict) {
        
        NSMutableArray *respArray = [NSMutableArray array];
        NSArray *resp = dict[@"resp"];
        for (NSDictionary *dic in resp) {
            StoreBookModel *model = [StoreBookModel yy_modelWithDictionary:dic];
            model.sortName = [[NativeSortNameManage shareInstance] getSortnameWithSortid:model.Sortid];
            [respArray addObject:model];
        }
        _dataArray = respArray;
        [self reloadData];
        
    } failed:^(int stateCode, NSString *failMsg) {
        
    }];
}

-(int)bookCount{
    
    int count = 0;
   
    switch (_layOutType.type) {
        
        case 1:
        case 4:
            count = _layOutType.rows*(isIpad?6:4);
            break;
        case 2:
        case 5:
            count = _layOutType.rows;
            break;
        case 3:
            count = _layOutType.rows+(isIpad?5:3);//第一行是横排的
            break;
            
        default:
            break;
    }
    
    return count;
}

-(void)reloadData{

}

-(void)moreBtnClick:(UIButton *)btn{
    MTLog(@"点击更多");
}

+(float)viewHeight:(LayoutType *)layoutType{
    
    return 0;
}

@end
